package com.central.apps.service.impl;

import com.central.apps.service.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class CommonServiceImpl implements CommonService {


    private  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    @Override
    public  Date getActualWeekday(Map<String, Object> week, Date beforeDate,boolean isBefore) throws Exception {
        Calendar c = Calendar.getInstance();
        String value ="";
        boolean isAction = true;
        int i = 0;
        while(isAction){
            i += 1;
            if(i>=7){
                isAction = false;
            }
            c.setTime(beforeDate);


            int nowWeek = dayForWeek(beforeDate);

            for(String key : week.keySet()){
                value = (String) week.get(nowWeek+"");

            }

            if(value.equals("1")){
                isAction = false;
                return beforeDate;
            }else{
                if(isBefore){
                    c.add(Calendar.DATE,-1);
                }else{
                    c.add(Calendar.DATE,1);
                }


                Date beforeODate = c.getTime();
                log.info("=============================beforeODate"+sdf.format(beforeODate));
                return getActualWeekday( week,beforeODate,isBefore);
            }
        }

        return new Date();
    }

    @Override
    public boolean nowDateIsActivity(Map<String, Object> week, Date pmNextDate) throws Exception {
        String value ="";
        int nowWeek = dayForWeek(pmNextDate);

        for(String key : week.keySet()){
            value = (String) week.get(nowWeek+"");

        }
        if(value.equals("1")){
            return true;
        }

        return false;
    }


    public  int dayForWeek(Date pTime) throws Exception {


        Calendar c = Calendar.getInstance();
        c.setTime(pTime);
        int dayForWeek = 0;

            dayForWeek = c.get(Calendar.DAY_OF_WEEK)-1;
        if((c.get(Calendar.DAY_OF_WEEK)-1)==0){
            dayForWeek=7;
        }
        return dayForWeek;
    }

    /*public String getNextexDate(Date exDate,String frequency,String cycle){
        log.info("===================cycle: "+cycle);
        int fre = Integer.parseInt(frequency);
        int unitTime = 0;
        if("D".equals(cycle)){
            unitTime = 1;
        }
        if("W".equals(cycle)){
            unitTime = 7;
        }
        if("M".equals(cycle)){
            unitTime = 30;
        }
        if("Y".equals(cycle)){
            unitTime = 365;
        }

        Calendar ca = Calendar.getInstance();
        ca.setTime(exDate);
        ca.add(Calendar.DAY_OF_MONTH, fre*unitTime);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return sdf.format(ca.getTime());
    }*/
}
